-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor contact forces sum in api.ode.system_velocity_dynamics
#180
Conversation
9f7cdf5
to
4eb6d47
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What was the original desiderata of this PR? Runtime performance, memory footprint, readability, etc?
It seems to me that now readability is much worse than before, and on a quick test I've done, the new logic seems wrong. Did you double check it?
src/jaxsim/api/ode.py
Outdated
W_f_Li_terrain = jnp.where( | ||
( | ||
parent_link_index_of_collidable_points[:, None] | ||
== jnp.arange(model.number_of_links()) | ||
).any(axis=-1, keepdims=True), | ||
W_f_Ci, | ||
jnp.zeros_like(W_f_Ci), | ||
).sum(axis=0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea was both to make the code simpler, as the current version is quite intricate, and reduce the memory footprint of that part of code. If you prefer, I can split the binary mask and the jnp.where(...)
as:
W_f_Li_terrain = jnp.where( | |
( | |
parent_link_index_of_collidable_points[:, None] | |
== jnp.arange(model.number_of_links()) | |
).any(axis=-1, keepdims=True), | |
W_f_Ci, | |
jnp.zeros_like(W_f_Ci), | |
).sum(axis=0) | |
mask = ( | |
parent_link_index_of_collidable_points[:, None] | |
== jnp.arange(model.number_of_links()) | |
).any(axis=-1, keepdims=True) | |
W_f_Li_terrain = jnp.where( | |
mask, | |
W_f_Ci, | |
jnp.zeros_like(W_f_Ci), | |
).sum(axis=0) |
I answered you with a comment in the code. For what regards instead the logic, in which example did you get the error? I did the test simulating spheres in |
Check the following snippet, I get different results with the new logic. Did I do any mistake? Codeimport jax
import jax.numpy as jnp
import jaxsim.api as js
import resolve_robotics_uri_py
import rod
# Find the urdf file.
urdf_path = resolve_robotics_uri_py.resolve_robotics_uri(
uri="model://ergoCubSN001/model.urdf"
)
# Build the ROD model.
rod_sdf = rod.Sdf.load(sdf=urdf_path)
# Build the model.
model = js.model.JaxSimModel.build_from_model_description(
model_description=rod_sdf.model,
)
# Get the parent body of the collidable points.
parent_link_index_of_collidable_points = jnp.array(
model.kin_dyn_parameters.contact_parameters.body
)
# Create the 6D forces of collidable points.
nc = len(parent_link_index_of_collidable_points)
W_f_Ci = jnp.ones(shape=(nc, 6))
# =========
# Old logic
# =========
f_old = jax.vmap(
lambda nc: (
jnp.vstack(jnp.equal(parent_link_index_of_collidable_points, nc).astype(int))
* W_f_Ci
).sum(axis=0)
)(jnp.arange(model.number_of_links()))
# =========
# New logic
# =========
f_new = jnp.where(
(
parent_link_index_of_collidable_points[:, None]
== jnp.arange(model.number_of_links())
).any(axis=-1, keepdims=True),
W_f_Ci,
jnp.zeros_like(W_f_Ci),
).sum(axis=0) |
4eb6d47
to
1d521ad
Compare
I found another way to further simplify the code, if you prefer I could merge the two lines. Ready for review @diegoferigo, thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much better. I also tested on the script of my previous comment and it seems working fine now.
Co-authored-by: Alessandro Croci <[email protected]>
Co-authored-by: Diego Ferigo <[email protected]>
8fa2707
to
69c77b1
Compare
This PR refactors contact forces sum in
system_velocity_dynamics
potentially improving readability and performance📚 Documentation preview 📚: https://jaxsim--180.org.readthedocs.build//180/